﻿using System;
using Microsoft.TV2.Tools;

namespace UDS.DB.Lib
{
    internal class CmdArgs : ICmdArgs
    {
        [Arg("droptable", "dt", "Drop existing tables.")]
        public bool DropTables
        {
            get;
            private set;
        }

        [Arg("dropview", "dv", "Drop existing views.")]
        public bool DropViews
        {
            get;
            private set;
        }

        [Arg("dropsp", "ds", "Drop existing stored procedures. Need to be explicitly specified.")]
        public bool DropStoredProcedures
        {
            get;
            private set;
        }

        [Arg("dropfunc", "df", "Drop existing functions. Need to be explicitly specified.")]
        public bool DropFunctions
        {
            get;
            private set;
        }

        [Arg("createview", "v", "Create views.")]
        public bool CreateViews
        {
            get;
            private set;
        }

        [Arg("createtable", "t", "Create tables.")]
        public bool CreateTables
        {
            get;
            private set;
        }

        [Arg("updatesp", "sp", "Create/update stored procedures.")]
        public bool UpdateStoredProcedures
        {
            get;
            private set;
        }

        [Arg("updatefunc", "f", "Create/update functions.")]
        public bool UpdateFunctions
        {
            get;
            private set;
        }

        [Arg("rebuild", "r", "Rebuild all database objects.")]
        public bool Rebuild
        {
            get;
            private set;
        }

        [Arg("server", "s", Description = "Use this option to override the database server name specified in config file.", Required = false, RequiredValue = ArgRequiredValue.Yes)]
        public string Server
        {
            get;
            private set;
        }

        #region ICmdArgs Members

        public bool ShowUsage
        {
            get;
            set;
        }

        public void Usage(string errorInfo)
        {
            if (!string.IsNullOrEmpty(errorInfo))
            {
                Console.WriteLine();
                Console.WriteLine(errorInfo);
                Console.WriteLine();
            }
            Console.WriteLine("Usage:");
            Console.WriteLine(ArgParser.Usage(this));
        }

        public void Validate()
        {
            if (!ShowUsage)
            {
                // At least one of the options should be specified.
                if (!DropTables && !DropViews && !DropStoredProcedures && !DropFunctions
                    && !CreateViews && !CreateTables && !UpdateStoredProcedures && !UpdateFunctions && !Rebuild)
                {
                    throw new InvalidArgException("", "No options were specified!");
                }
            }
        }

        public void ProcessStandAloneArgument(string arg)
        {
            throw new InvalidArgException(arg);
        }

        #endregion
    }
}